CC=gcc
Q=@

#Build Type
ver=release

CFLAGS:=-D_GNU_SOURCE -Wall -std=gnu11 -march=native -fno-strict-aliasing

#编译位置无关的代码
CFLAGS +=-fPIC

ifeq ($(ver), debug)
CFLAGS +=-g -O0  
else
CFLAGS +=-DWY_NDEBUG -O3
endif


#SPDK 项目目录
SPDK_DIR := /home/wuyue/spdk

#FIO 项目目录
FIO_DIR := /home/wuyue/fio


#适用于 SPDK-20.10 
DPDK_LIB := -lrte_eal -lrte_mempool -lrte_ring -lrte_mbuf -lrte_mempool_ring -lrte_pci
DPDK_LIB += -lrte_bus_pci -lrte_kvargs -lrte_vhost -lrte_net -lrte_hash -lrte_telemetry
DPDK_LIB += -lrte_cryptodev -lrte_power -lrte_rcu


SPDK_BDEV_MOUDLES := -lspdk_bdev_malloc -lspdk_bdev_null -lspdk_bdev_delay -lspdk_bdev_nvme

SPDK_LIB := $(SPDK_BDEV_MOUDLES) \
	-lspdk_event_bdev -lspdk_event_accel -lspdk_event_vmd -lspdk_event_sock -lspdk_bdev \
	-lspdk_accel -lspdk_event -lspdk_thread -lspdk_util -lspdk_conf -lspdk_trace \
	-lspdk_log -lspdk_json -lspdk_jsonrpc -lspdk_rpc -lspdk_sock -lspdk_nvme\
	-lspdk_notify -lspdk_vmd -lspdk_env_dpdk \
	$(DPDK_LIB) 

DPDK_LIB_DIR := $(SPDK_DIR)/dpdk/build/lib
SPDK_INC_DIR := $(SPDK_DIR)/include
SPDK_LIB_DIR := $(SPDK_DIR)/build/lib

SPDK_LINK_FLAGS :=-L$(DPDK_LIB_DIR) -L$(SPDK_LIB_DIR) \
	-Wl,--whole-archive,-Bstatic $(SPDK_LIB) 

SYS_LIBS := -lnuma -luuid -lpthread -ldl -lrt -lpmem
SYS_LINK_FLAGS := -Wl,--no-whole-archive,-Bdynamic $(SYS_LIBS)

INC_FLAGS := -I. -I$(SPDK_INC_DIR) -I$(FIO_DIR)


####


MAKEFLAGS += --no-print-directory

C_SRCS +=$(wildcard *.c)

C_SRCS +=spdk_bdev/bdev_zcell_rpc.c  spdk_bdev/bdev_zcell.c

OBJS = $(C_SRCS:.c=.o)


DEPFLAGS = -MMD -MP -MF $*.d.tmp

# Compile first input $< (.c) into $@ (.o)
COMPILE_C=\
	$(Q)echo "  CC [$(ver)] $@";\
	$(CC) -o $@ $(INC_FLAGS) $(DEPFLAGS)  $(CFLAGS) -c $< && \
	mv -f $*.d.tmp $*.d && touch -c $@

# Link $^ and $(LIBS) into $@ (app/lib)
LINK_APP=\
	$(Q)echo "  LINK_APP [$(ver)] $@"; \
	$(CC) -o $@ $(INC_FLAGS) $(CFLAGS)  $^ $(SPDK_LINK_FLAGS) $(SYS_LINK_FLAGS)

LINK_SHARED_LIB=\
	$(Q)echo "  LINK_SHARED_LIB [$(ver)] $@"; \
	$(CC) -shared -o $@ $(INC_FLAGS) $(CFLAGS) $^ $(SPDK_LINK_FLAGS) $(SYS_LINK_FLAGS)


MSGR_OBJS = messager.o net.o net_posix.o spdk_ipc_messager.o
OSTORE_OBJS = objectstore.o chunkstore.o nullstore.o zstore.o pm.o
LIBOSS_OBJS = liboss.o 
ZCELL_BDEV_OBJS = spdk_bdev/bdev_zcell_rpc.o  spdk_bdev/bdev_zcell.o liboss.o  $(MSGR_OBJS)

ALL := server bdev_fio_plugin bdev_perf

.PHONY: all clean 


all : $(ALL)

server : server_main.o $(OSTORE_OBJS) $(MSGR_OBJS)
	$(LINK_APP)

bdev_fio_plugin: bdev_fio_plugin.o $(ZCELL_BDEV_OBJS)
	$(LINK_SHARED_LIB)

bdev_perf:bdev_perf.o $(ZCELL_BDEV_OBJS)
	$(LINK_APP)
# foo :
# 	echo $(SPDK_DIR)
# 	echo $(C_SRCS)
# 	echo $(CFLAGS)

%.o: %.c %.d
	$(COMPILE_C)

%d: ;

.PRECIOUS: $(OBJS)

-include $(OBJS:.o=.d)


clean:
	@rm -rf *.o core* *.d

